/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Negocio.Artigo;
import Negocio.CadastroException;
import Persistencia.DAOException;
import javax.swing.JOptionPane;

/**
 *
 * @author asouza
 */
public class FLeilao extends javax.swing.JInternalFrame {
    
    CadastroControlador controlador;
    /**
     * Creates new form FLeilao
     */
     public FLeilao() throws CadastroException, DAOException,Exception{
        controlador = new CadastroControlador();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListAberto = new javax.swing.JList();
        jButtonParticarLeilao = new javax.swing.JButton();
        jButtonParticarLeilao1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListFechado = new javax.swing.JList();
        jButtonConsultarFechado = new javax.swing.JButton();

        setClosable(true);
        setPreferredSize(new java.awt.Dimension(777, 538));

        jListAberto.setModel(controlador.getListaArtigoAberto());
        jScrollPane1.setViewportView(jListAberto);

        jButtonParticarLeilao.setText("Participar");
        jButtonParticarLeilao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonParticarLeilaoActionPerformed(evt);
            }
        });

        jButtonParticarLeilao1.setText("Fechar Leilão");
        jButtonParticarLeilao1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonParticarLeilao1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 736, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jButtonParticarLeilao1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(533, 533, 533)
                        .addComponent(jButtonParticarLeilao, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 395, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonParticarLeilao1)
                    .addComponent(jButtonParticarLeilao))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Aberto(s)", jPanel1);

        jListFechado.setModel(controlador.getListaArtigoFechado());
        jScrollPane2.setViewportView(jListFechado);

        jButtonConsultarFechado.setText("Consultar");
        jButtonConsultarFechado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultarFechadoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 736, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(0, 648, Short.MAX_VALUE)
                        .addComponent(jButtonConsultarFechado, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 399, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonConsultarFechado)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Fechado(s)", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 761, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 503, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonParticarLeilaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonParticarLeilaoActionPerformed
        // TODO add your handling code here:
        try{
           Artigo aSelecionado =(Artigo) jListAberto.getSelectedValue(); 
           if(aSelecionado == null)
           {
                JOptionPane.showMessageDialog(this, "Selecione um Artigo para particpar do leilão.");
                return;
           }
           FLance f = new FLance(aSelecionado);
           f.setLocationRelativeTo(null);
           f.setVisible(true);
        }catch (CadastroException ex) {
                    System.out.println("Erro fatal: " + ex.getMessage());
                    System.out.println(ex.toString());
                    System.exit(0);
        } catch(DAOException e){
             System.out.println(e.toString());
        } catch (Exception e){
            System.out.println(e.toString());
        }
        
    }//GEN-LAST:event_jButtonParticarLeilaoActionPerformed

    private void jButtonConsultarFechadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultarFechadoActionPerformed
        // TODO add your handling code here:
        try{ 
           Artigo aSelecionado =(Artigo) jListFechado.getSelectedValue(); 
           if(aSelecionado == null)
           {
                JOptionPane.showMessageDialog(this, "Selecione um Artigo para consultar lances.");
                return;
           }
           FLanceFechado form = new FLanceFechado(aSelecionado);
           form.setLocationRelativeTo(null);
           form.setVisible(true);
        }catch (CadastroException ex) {
                    System.out.println("Erro fatal: " + ex.getMessage());
                    System.out.println(ex.toString());
                    System.exit(0);
                } catch(DAOException e){
                     System.out.println(e.toString());
                }catch (Exception e){
                    JOptionPane.showMessageDialog(this, "O Leilão fechou e não deram lances");
                }
    }//GEN-LAST:event_jButtonConsultarFechadoActionPerformed

    private void jButtonParticarLeilao1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonParticarLeilao1ActionPerformed
        // TODO add your handling code here:
        try
        {
           Artigo aSelecionado =(Artigo) jListAberto.getSelectedValue(); 
           if(aSelecionado == null)
           {
                JOptionPane.showMessageDialog(this, "Selecione um Artigo para consultar lances.");
                return;
           }
           if(controlador.fecharLeilao(aSelecionado.getId()))
           {
              JOptionPane.showMessageDialog(this, "Encerrado com sucesso, favor atualizar a tela");
           }else
             JOptionPane.showMessageDialog(this, "Opa, não encontrou o item");
        }catch(DAOException d)
        {
            JOptionPane.showMessageDialog(this, ""+ d.getMessage());
        }
                
                
    }//GEN-LAST:event_jButtonParticarLeilao1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConsultarFechado;
    private javax.swing.JButton jButtonParticarLeilao;
    private javax.swing.JButton jButtonParticarLeilao1;
    private javax.swing.JList jListAberto;
    private javax.swing.JList jListFechado;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
}
